package com.hghivln.controller.doctor;


import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.constants.Constants;
import com.hghivln.pojo.domain.User;
import com.hghivln.pojo.dto.CareHistoryDto;
import com.hghivln.pojo.dto.CareOrderFormDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.pojo.vo.CareHistoryVo;
import com.hghivln.service.CareService;
import com.hghivln.service.PatientService;
import com.hghivln.service.RegistrationService;
import com.hghivln.utils.HisDateUtils;
import com.hghivln.utils.ShiroUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("doctor/care")
public class CareController {

    @Reference
    private RegistrationService registrationService;

    @Reference
    private PatientService patientService;

    @Reference
    private CareService careService;

    /**
     * 加载挂号列表[待就诊]
     */
    @GetMapping("queryToBeSeenRegistration/{schedulingType}")
    @HystrixCommand
    public AjaxResult queryToBeSeenRegistration(@PathVariable @Validated @NotBlank(message = "挂号类型不能为空") String schedulingType) {
        Long deptId = ShiroUtils.getCurrentUser().getDeptId();
        String subsectionType = HisDateUtils.getSubsectionType();
        return registrationService.queryRegistration(schedulingType, deptId, Constants.REG_STATUS_1, subsectionType);
    }

    /**
     * 加载挂号列表[就诊中]
     */
    @GetMapping("queryVisitingRegistration/{schedulingType}")
    @HystrixCommand
    public AjaxResult queryVisitingRegistration(@PathVariable @Validated @NotBlank(message = "挂号类型不能为空") String schedulingType) {
        Long deptId = ShiroUtils.getCurrentUser().getDeptId();
        return registrationService.queryRegistration(schedulingType,deptId, Constants.REG_STATUS_2,null);
    }

    /**
     * 加载挂号列表[就诊完成]
     */
    @GetMapping("queryVisitCompletedRegistration/{schedulingType}")
    @HystrixCommand
    public AjaxResult queryVisitCompletedRegistration(@PathVariable @Validated @NotBlank(message = "挂号类型不能为空") String schedulingType) {
        Long deptId = ShiroUtils.getCurrentUser().getDeptId();
        return registrationService.queryRegistration(schedulingType,deptId, Constants.REG_STATUS_3, null);
    }

    /**
     * 接诊
     */
    @PostMapping("receivePatient/{regId}")
    @HystrixCommand
    @Log(title = "接诊",businessType = BusinessType.UPDATE)
    public AjaxResult receivePatient(@PathVariable @Validated @NotBlank(message = "挂号单号不能为空") String regId) {
        User user = ShiroUtils.getCurrentUser();
        return patientService.receivePatient(regId,user);
    }

    /**
     * 根据患者ID查询患者信息 患者档案信息  历史病例
     */
    @GetMapping("getPatientAllMessageByPatientId/{patientId}")
    @HystrixCommand
    public AjaxResult getPatientAllMessageByPatientId(@PathVariable @Validated @NotBlank(message = "就诊卡号不能为空") String patientId) {
        return patientService.getPatientAllMessageByPatientId(patientId);
    }

    /**
     *  保存病历的接口
     */
    @PostMapping("saveCareHistory")
    @HystrixCommand
    @Log(title = "保存病历的接口",businessType = BusinessType.INSERT)
    public AjaxResult saveCareHistory(@RequestBody @Validated CareHistoryDto careHistoryDto) {
        User user = ShiroUtils.getCurrentUser();
        return careService.saveOrUpdateCareHistory(careHistoryDto,user);
    }

    /**
     * 根据挂号ID查询病历信息
     */
    @GetMapping("getCareHistoryByRegId/{regId}")
    @HystrixCommand
    public AjaxResult getCareHistoryByRegId(@PathVariable @Validated @NotBlank(message = "挂号单号不能为空") String regId) {
        CareHistoryVo careHistoryVo = careService.selectCareHistoryByRegId(regId);
        return AjaxResult.success(careHistoryVo);
    }

    /**
     * 根据病例ID查询处方列表及详情
     */
    @GetMapping("queryCareOrdersByChId/{chId}")
    @HystrixCommand
    public AjaxResult queryCareOrdersByChId(@PathVariable @Validated @NotBlank(message = "病例单号不能为空") String chId) {
        return careService.queryCareOrdersByChId(chId);
    }

    /**
     * 保存处方及详情信息
     */
    @HystrixCommand(commandProperties = { // 由于服务的响应超过1秒，会报超时错误，所以修改响应超时的时间
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000")
    })
    @PostMapping("saveCareOrderItem")
    @Log(title = "保存处方及详情信息",businessType = BusinessType.INSERT)
    public AjaxResult saveCareOrderItem(@RequestBody @Validated CareOrderFormDto careOrderFormDto) {
        careOrderFormDto.setUser(ShiroUtils.getCurrentUser());
        return careService.saveCareOrderItem(careOrderFormDto);
    }

    /**
     * 根据处方详情ID删除处方详情【只能删除未支付的】
     */
    @DeleteMapping("deleteCareOrderItemById/{itemId}")
    @HystrixCommand
    @Log(title = "根据处方详情ID删除处方详情【只能删除未支付的】",businessType = BusinessType.DELETE)
    public AjaxResult deleteCareOrderItemById(@PathVariable @Validated @NotBlank(message = "处方详情ID不能为空") String itemId) {
        return careService.deleteCareOrderItemById(itemId);
    }

    /**
     * 根据挂号单号完成就诊
     */
    @PostMapping("visitComplete/{regId}")
    @HystrixCommand
    @Log(title = "根据挂号单号完成就诊",businessType = BusinessType.UPDATE)
    public AjaxResult visitComplete(@PathVariable @Validated @NotBlank(message = "挂号单号不能为空") String regId) {
        return careService.visitComplete(regId);
    }
}


